<?php


namespace app\admin\controller;


use app\admin\common\Base;
use think\Db;
use think\facade\Request;

class Article extends Base
{
    use \app\admin\traits\BaseFn;//引入公共方法(增删改查)  具体看traits

    protected $model = null;

    public function initialize(){
        parent::initialize();
        $this->model = new \app\admin\model\Article;
    }
    /**
     * 列表
     */
    public function index(){
        if(Request::isAjax()){
            list($where,$sort) = $this->buildQuery();
            $list = $this->model
                ->where($where)
                ->order($sort)
                ->paginate(Request::param('pageSize'))
                ->each(function ($item,$key){
                    $roles = Db::name('category')->where([['id','in',$item['catid']]])->column('catname');
                    $item['catname'] = '';
                    if($roles)
                        $item['catname'] = implode(',',$roles);
                    return $item;
                });

            $list = $list->toArray();
            $data = [
                "total"=>$list['total'],
                'rows'=>$list['data'],
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * 添加
     */
    public function add(){
        if(Request::isAjax()){
            $code = 0;
            $msg = 'success';
            $data = null;
            try{
                $post = Request::param();
                $post['create_time'] = time();
                $insert = $this->model->allowField(true)->save($post);//过滤数据库字段写入

                if($insert){
                    $msg = "添加成功";
                }else{
                    $code = 10001;
                    throw new Exception('添加失败');
                }

            }catch (Exception $e){
                $msg = $e->getMessage();
            }

            return json(['data'=>$data,'msg'=>$msg,'code'=>$code]);
        }
        //获取栏目列表
        $cate = Db::name('Category')->select();
        $cate = getTree($cate,'catname');
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    /**
     * 编辑
     */
    public function edit(){
        if(Request::isAjax()){
            $code = 0;
            $msg = 'success';
            $data = null;
            try{
                $where = [
                    'id'=>Request::param('id'),
                ];
                $update = Request::except('id');

                $this->model->save($update,$where);
                $msg = "保存成功";

            }catch (Exception $e){
                $msg = $e->getMessage();
            }

            return json(['data'=>$data,'msg'=>$msg,'code'=>$code]);
        }
        $where = Request::param();
        $data = $this->model->where($where)->find();
        $this->assign('data',$data);
        //获取栏目列表
        $cate = Db::name('Category')->select();
        $cate = getTree($cate,'catname');
        $this->assign('cate',$cate);
        return $this->fetch();
    }
}